package com.ruoyi.miniapp.controller;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.miniapp.api.domain.MiniappDept;
import com.ruoyi.miniapp.service.IMiniappDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门Controller
 *
 * @author ruoyi
 * @date 2023-09-17
 */
@RestController
@RequestMapping("/dept")
public class MiniappDeptController extends BaseController
{
    @Autowired
    private IMiniappDeptService miniappDeptService;

    /**
     * 查询部门列表
     */
    @RequiresPermissions("miniapp:dept:list")
    @GetMapping("/list")
    public AjaxResult list(MiniappDept miniappDept)
    {
        startPage();
        List<MiniappDept> list = miniappDeptService.selectMiniappDeptList(miniappDept);
        return success(list);
    }


    /**
     * 获取部门详细信息
     */
    @RequiresPermissions("miniapp:dept:query")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        miniappDeptService.checkDeptDataScope(deptId);
        return success(miniappDeptService.selectMiniappDeptByDeptId(deptId));
    }

    /**
     * 新增部门
     */
    @RequiresPermissions("miniapp:dept:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniappDept miniappDept)
    {
        if (!miniappDeptService.checkDeptNameUnique(miniappDept))
        {
            return error("新增部门'" + miniappDept.getDeptName() + "'失败，部门名称已存在");
        }
        miniappDept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(miniappDeptService.insertMiniappDept(miniappDept));
    }

    /**
     * 修改部门
     */
    @RequiresPermissions("miniapp:dept:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniappDept miniappDept)
    {
        Long deptId = miniappDept.getDeptId();
        miniappDeptService.checkDeptDataScope(deptId);
        if (!miniappDeptService.checkDeptNameUnique(miniappDept))
        {
            return error("修改部门'" + miniappDept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (miniappDept.getParentId().equals(deptId))
        {
            return error("修改部门'" + miniappDept.getDeptName() + "'失败，上级部门不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, miniappDept.getStatus()) && miniappDeptService.selectNormalChildrenDeptById(deptId) > 0)
        {
            return error("该部门包含未停用的子部门！");
        }
        miniappDept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(miniappDeptService.updateMiniappDept(miniappDept));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("miniapp:dept:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long deptIds)
    {
        if (miniappDeptService.hasChildByDeptId(deptIds))
        {
            return warn("存在下级部门,不允许删除");
        }
        if (miniappDeptService.checkDeptExistUser(deptIds))
        {
            return warn("部门存在用户,不允许删除");
        }
        miniappDeptService.checkDeptDataScope(deptIds);
        return toAjax(miniappDeptService.deleteMiniappDeptByDeptId(deptIds));
    }
}
